import * as types from '../types'
import MsgSession from '../../util/socket/msg/MsgSession'
import MsgCenter from '../../util/socket/msg/MsgCenter'
import MsgRecord from '../../util/socket/msg/MsgRecord'

export default {
    /**
     * 收到消息时候新增或者修改session
     * 下载session记录的时候合并
     * 如果当前session的用户信息没有 就去下载
     * @param state
     * @param firstUrl
     */
    [types.UPDATE_SESSION_MAP](state, {type, sessionList, msgSession}) {
        if (type == 'push') { //新收到一条消息
            state.sessionList = MsgSession.pushSession(state.sessionMap,msgSession)

        } else if (type == 'merge') { //下载历史与当前合并
            state.sessionList = MsgSession.mergeSessions(state.sessionMap, sessionList)

        }

    },


    /**
     * 合并msgMap
     * @param state
     * @param firstUrl
     */
    [types.UPDATE_MSG_MAP](state,{msg, type, msgList, sessionId}) {
        if (type == 'push') { //收到一条消息
            MsgRecord.pushMsg(sessionId, state.msgMap, msg)
        } else if (type == 'megre') { //正在与人聊天的时候下载大量聊天记录
            MsgRecord.mergeMsg(sessionId,state.msgMap,msgList)
        }
    },


    /**
     * 更细当前消息
     * @param state
     * @param msg
     */
    [types.UPDATE_CURRENT_MSG_LIST](state, {sessionId, msg, type, msgList}) {
        if (type == 'init') { //初始化
            state.currentMsgList = MsgRecord.initCurrentMsg(sessionId, state.msgMap)
        } else if (type == 'push') { //收到新的消息 加入当前消息列表
            state.currentMsgList.push(msg)
        } else if (type == 'concat') { //历史消息加入当前 消息列表
            state.currentMsgList = MsgRecord.concatCurrentMsg(state.currentMsgList, msgList)

        }
    },

    /**
     * 更新用户基本信息
     * @param state
     * @param userInfoMap
     */
    [types.UPDATE_USER_INFO_MAP](state, userInfoList) {
        if(!userInfoList)return
        let userInfoMap = state.userInfoMap
        for (let i = 0, j = userInfoList.length; i < j; i++) {
            let userInfo = userInfoList[i]
            userInfoMap[userInfo.id] = userInfo
        }
        //更新Vuex的时候 一定要返回一个新的对象
        state.userInfoMap = Object.assign({}, userInfoMap)
    },

    /**
     * 更新群聊基本信息
     * @param state
     * @param groupInfoList
     */
    [types.UPDATE_GROUP_INFO_MAP](state,groupInfoList){
        if(!groupInfoList)return
      let groupInfoMap = state.groupInfoMap
        for (let i = 0, j = groupInfoList.length; i < j; i++) {
            let groupInfo = groupInfoList[i]
            groupInfoMap[groupInfo.id] = groupInfo
        }
        console.log(groupInfoMap)
        //更新Vuex的时候 一定要返回一个新的对象 才会更新
        state.groupInfoMap = Object.assign({}, groupInfoMap)
    },

    /**
     * 设置session已经初始化了
     * @param state
     * @param sessionId
     */
    [types.SET_SESSION_INIT](state, {sessionId}) {
        let sessionMap = state.sessionMap
        let session = sessionMap[sessionId]
        session.init = true
        state.sessionMap = Object.assign({}, sessionMap)
    }


}